<?php
class ExclusiveCodeLog extends BaseModel
{
    protected $res;
    public $key;
    public function getSource()
    {
        return "exclusive_code_log";
    }

    public function getlist($params){
        $conditions = '';
        if((isset($params['start_time']) && !empty($params['start_time'])) || (isset($params['end_time']) && !empty($params['end_time']))){
            if($conditions){
                $conditions .= " AND (ExclusiveCodeLog.created_at >= '{$params['start_time']}' AND ExclusiveCodeLog.created_at <= '{$params['end_time']}')";
            }else{
                $conditions .= " WHERE (ExclusiveCodeLog.created_at >= '{$params['start_time']}' AND ExclusiveCodeLog.created_at <= '{$params['end_time']}')";
            }
        }
        if(isset($params['status']) && !empty($params['status'])){
            if($conditions){
                $conditions .= " AND ExclusiveCodeLog.status={$params['status']}";
            }else{
                $conditions .= " WHERE ExclusiveCodeLog.status={$params['status']}";
            }
        }
        if(isset($params['user_ids']) && !empty($params['user_ids'])){
            if($conditions){
                $conditions .= " AND ExclusiveCodeLog.user_id in({$params['user_ids']})";
            }else{
                $conditions .= " WHERE ExclusiveCodeLog.user_id in({$params['user_ids']})";
            }
        }
        $phql = "SELECT ExclusiveCodeLog.id,ExclusiveCodeLog.user_id,ExclusiveCodeLog.price,ExclusiveCodeLog.exclusive_code,ExclusiveCodeLog.status,ExclusiveCodeLog.order_sn,
                 ExclusiveCodeLog.created_at,Member.phone,Member.nickname,Member.avatar,Member.invite_code
                FROM ExclusiveCodeLog 
                LEFT JOIN Member ON ExclusiveCodeLog.user_id = Member.id
                {$conditions} 
                ORDER BY ExclusiveCodeLog.id DESC";
        $rows = $this->modelsManager->executeQuery($phql);
        return $rows;
    }


    /**
     * @param $id根据提现id获取提现记录
    */
    public function find_by_id($id){
        $withdraw_info = $this->findFirstById($id)->toarray();
        if($withdraw_info){
            return $withdraw_info;
        }else{
            return [];
        }
    }


    public function save_withdraw($params){
        return $this::save($params);
    }


}
